const QQMapWX = require('../../pages/cesi2/qqmap-wx-jssdk1.2/qqmap-wx-jssdk');
// 实例化API核心类
const qqmapsdk = new QQMapWX({
  key: 'NCLBZ-6X5LZ-7KAXO-TCS7S-RFMNZ-KVBH4' // 你的腾讯地图开发者密钥
});

Page({
  data: {
    scale: 16,
    markers: [],
    longitude: null,
    latitude: null
  },

  onShow() {
    this.mapCtx = wx.createMapContext('map');
    this.returnToCurrentLocation();
  },

  bindRegionChange(e) {
    if (e.type === 'end') {
      this.mapCtx.getCenterLocation({
        success: (res) => {
          this.getHotels(res.longitude, res.latitude);
        },
        fail: (res) => {
          console.log("搜索失败");
        }
      });
    }
  },

  getHotels(longitude, latitude) {
    qqmapsdk.search({
      keyword: '药店',
      location: {
        longitude: longitude,
        latitude: latitude
      },
      success: (res) => {
        const markers = res.data.map((hotel, index) => {
          const distance = this.calculateDistance(latitude, longitude, hotel.location.lat, hotel.location.lng);
          return {
            iconPath: '../../images/定位.png',
            id: index,
            latitude: hotel.location.lat,
            longitude: hotel.location.lng,
            width: 30,
            height: 30,
            title: hotel.title, // 添加药店昵称信息
            callout: {
              content: `${hotel.title}\n距离: ${distance}米`,
              color: '#ffffff',
              fontSize: 14,
              borderRadius: 5,
              padding: 8,
              bgColor: '#000000',
              display: 'ALWAYS',
              anchorY: -50
            }
          }
        });

        markers.push({
          iconPath: '../../images/定位.png',
          id: res.data.length,
          longitude: longitude,
          latitude: latitude,
          width: 30,
          height: 30
        });

        this.setData({
          markers: markers
        });
      },
      fail: (res) => {
        console.log(res);
      }
    });
  },

  returnToCurrentLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        });
        this.getHotels(res.longitude, res.latitude);
      }
    });
    this.mapCtx.moveToLocation();
  },

  onLoad(options) {
  },

  onReady: function () {
    wx.getLocation({
      type: 'gcj02',
      success: res => {
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        });
      }
    });
  },

  navigateToHotel: function(e) {
    var markerId = e.detail.markerId;
    var marker = this.data.markers[markerId];
    var latitude = marker.latitude;
    var longitude = marker.longitude;

    wx.openLocation({
      latitude: latitude,
      longitude: longitude
    });
  },


  calculateDistance(lat1, lon1, lat2, lon2) {
    const R = 6371e3; // Earth's radius in meters
    const φ1 = lat1 * Math.PI / 180; // φ, λ in radians
    const φ2 = lat2 * Math.PI / 180;
    const Δφ = (lat2 - lat1) * Math.PI / 180;
    const Δλ = (lon2 - lon1) * Math.PI / 180;
    const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
      Math.cos(φ1) * Math.cos(φ2) *
      Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    const d = R * c; // distance in meters
    return d.toFixed(2); // round to 2 decimal places
  }
});